﻿/**
 * HiScoresTable.cs - Non-visual data structure to store high scores
 * 
 * Part of BoulderDAM - A BoulderDash clone using C# and SDL
 * Nacho Cabanes & students of DAM IES San Vicente, 2014
 * 
 * Revisions
 * #     Date         Creator, changes
 * ----  -----------  -------------------------------------------------------
 * 0.01  02-apr-2014  Nacho Cabanes: Skeleton
 * 0.02  03-apr-2014  Boro, Rei, Ramon, David : read file text, compare the last position, sort the array;
 * 0.04  07-apr-2014  Alex López, Álvaro Gironés, Saúl Sánchez: Filling zeros when "score" is less than 4 digits
 */

using System;
using System.IO;
using System.Collections.Generic;

namespace Boulder
{
    class HiScoresTable
    {
        protected StreamReader file;
        protected string aux;
        protected string[] records = new string[10];
        protected int contador = -1;

        // constructor that reads the data file records 

        public HiScoresTable()
        {
            file = File.OpenText("records.txt");
            do
            {
                aux = file.ReadLine();
                if (aux != null)
                {
                    contador++;
                    records[contador] = aux;
                }

            } while (aux != null);
            file.Close();
        }


        // Add a new HighScore (name + score) to the table
        public void AddHiScore(string name, int score)
        {
            // compares the last saved record

            if (Convert.ToInt32(records[9].Substring(6, 5)) < score)
            {
                string score2 = Convert.ToString(score);

                while (score2.Length < 5)
                {
                    score2 += "0" + score2;
                }

                // replace the last position
                records[9] = "temp " + score + " " + name;

                //sort array
                for (int i = 1; i < 10; i++)
                {
                    int j = i - 1;
                    while ((j >= 0) && (Convert.ToInt32(records[j].Substring(6, 5))
                        < Convert.ToInt32(records[j + 1].Substring(6, 5))))
                    {
                        string temporal = records[j];
                        records[j] = records[j + 1];
                        records[j + 1] = temporal;
                        j--;
                    }
                }
            }



        }


        // Amount of high scores
        public int Length { get; set; }


        // Name of a high score
        public string GetName(byte position)
        {
            // TO DO
            return "";
        }


        // Points of a high score
        public int GetScore(byte position)
        {
            // TO DO
            return 0;
        }
    }
}
